﻿namespace REVISIONMAX20
{
    using System;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;

    public class UIHandler : IDisposable
    {
        public DockPanel dockPanel = null;
        public frmGroupList groupPanel = new frmGroupList();

        public UIHandler(DockPanel pdockPanel)
        {
            this.dockPanel = pdockPanel;
            this.InitPanels();
        }

        private void InitPanels()
        {
            this.groupPanel.MdiParent = this.ParentForm;
            this.groupPanel.Show(this.dockPanel);
            this.groupPanel.uiHandler = this;
            this.groupPanel.DockState = DockState.DockLeft;
            this.groupPanel.VisibleState = DockState.DockLeft;
        }

        public void ShowGroups()
        {
            this.groupPanel.Show();
        }

        void IDisposable.Dispose()
        {
            this.groupPanel.Close();
            this.groupPanel.Dispose();
            this.groupPanel = null;
            this.dockPanel.Dispose();
            this.dockPanel = null;
        }

        public void ViewConnectionString(ConnectionString pConnectionString)
        {
            new frmConnectionString().View(this, pConnectionString);
        }

        public void ViewGroup(Group pGroup)
        {
            foreach (DockPane pane in this.dockPanel.Panes)
            {
                foreach (DockContent content in pane.Contents)
                {
                    if ((content is frmGroup) && (((frmGroup) content).id == pGroup.id))
                    {
                        content.BringToFront();
                        return;
                    }
                }
            }
            frmGroup group = new frmGroup();
            group.MdiParent = this.ParentForm;
            group.Show(this.dockPanel);
            group.DockState = DockState.Document;
            group.VisibleState = DockState.Document;
            group.View(this, pGroup);
        }

        public void ViewLog(Log pLog)
        {
            new frmLog().View(this, pLog);
        }

        public void ViewSource(Source pSource)
        {
            new frmSource().View(this, pSource);
        }

        public void ViewSQLItem(SQLItem pSQLItem)
        {
            new frmSQLItem().View(this, pSQLItem);
        }

        public Form ParentForm
        {
            get
            {
                return (Form) this.dockPanel.Parent;
            }
        }
    }
}

